//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SurvivalProbability.h"
using namespace Cephei::QL::Termstructures::Credit;
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Termstructures::Credit::CSurvivalProbability::CSurvivalProbability (boost::shared_ptr<QuantLib::SurvivalProbability>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSurvivalProbability = NULL;
#endif
	_ppSurvivalProbability = &childNative;
    
}
Cephei::QL::Termstructures::Credit::CSurvivalProbability::CSurvivalProbability (QuantLib::SurvivalProbability& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSurvivalProbability = NULL;
#endif
	_ppSurvivalProbability = new boost::shared_ptr<QuantLib::SurvivalProbability> (&childNative);
    
    _SurvivalProbabilityOwner = owner;
    
}

Cephei::QL::Termstructures::Credit::CSurvivalProbability::CSurvivalProbability (CSurvivalProbability^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSurvivalProbability = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSurvivalProbability = new boost::shared_ptr<QuantLib::SurvivalProbability> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Credit::CSurvivalProbability::CSurvivalProbability (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSurvivalProbability = NULL;
#endif
	if (!t->IsSubclassOf(CSurvivalProbability::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Credit::CSurvivalProbability::CSurvivalProbability (QuantLib::Handle<QuantLib::SurvivalProbability>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSurvivalProbability = &childNative;
	_ppSurvivalProbability = &static_cast<boost::shared_ptr<QuantLib::SurvivalProbability>>(childNative.currentLink());
    
    _SurvivalProbabilityOwner = owner;
}
Cephei::QL::Termstructures::Credit::CSurvivalProbability::CSurvivalProbability (QuantLib::Handle<QuantLib::SurvivalProbability> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSurvivalProbability = &childNative;
	_ppSurvivalProbability = &static_cast<boost::shared_ptr<QuantLib::SurvivalProbability>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Credit::CSurvivalProbability::CSurvivalProbability (QuantLib::SurvivalProbability childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSurvivalProbability = NULL;
#endif
	_ppSurvivalProbability = new boost::shared_ptr<QuantLib::SurvivalProbability> (new QuantLib::SurvivalProbability (childNative));
    
}
#endif

Cephei::QL::Termstructures::Credit::CSurvivalProbability::~CSurvivalProbability ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSurvivalProbability != NULL)
    {
	    delete _ppSurvivalProbability;
        _ppSurvivalProbability = NULL;
    }
}
Cephei::QL::Termstructures::Credit::CSurvivalProbability::!CSurvivalProbability ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSurvivalProbability != NULL)
    {
	    delete _ppSurvivalProbability;
    }
}
QuantLib::SurvivalProbability& Cephei::QL::Termstructures::Credit::CSurvivalProbability::GetReference ()
{
    if (_ppSurvivalProbability == NULL) throw REFNEW NativeNullException ();
	return **_ppSurvivalProbability;
}
boost::shared_ptr<QuantLib::SurvivalProbability>& Cephei::QL::Termstructures::Credit::CSurvivalProbability::GetShared ()
{
    if (_ppSurvivalProbability == NULL) throw REFNEW NativeNullException ();
	return *_ppSurvivalProbability;
}
QuantLib::SurvivalProbability* Cephei::QL::Termstructures::Credit::CSurvivalProbability::GetPointer ()
{
    if (_ppSurvivalProbability == NULL) throw REFNEW NativeNullException ();
	return &**_ppSurvivalProbability;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SurvivalProbability>& Cephei::QL::Termstructures::Credit::CSurvivalProbability::GetHandle ()
{
	if (_phSurvivalProbability == NULL)
	{
		_phSurvivalProbability = new Handle<QuantLib::SurvivalProbability> (*_ppSurvivalProbability);
	}
	return *_phSurvivalProbability;
}
#endif
bool Cephei::QL::Termstructures::Credit::CSurvivalProbability::HasNative () 
{
	return (_ppSurvivalProbability != NULL);
}

DateTime Cephei::QL::Termstructures::Credit::CSurvivalProbability::InitialDate (Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ c)
{
    CDefaultProbabilityTermStructure^ _Cc;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cc = safe_cast<CDefaultProbabilityTermStructure^> (c);
        _Cc->Lock();
        QuantLib::DefaultProbabilityTermStructure* _c = static_cast<QuantLib::DefaultProbabilityTermStructure*> (_Cc->GetPointer ()); 
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSurvivalProbability)->initialDate ( _c );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cc != nullptr) _Cc->Unlock();
    }
}
Double Cephei::QL::Termstructures::Credit::CSurvivalProbability::InitialValue (Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ prm1)
{
    CDefaultProbabilityTermStructure^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CDefaultProbabilityTermStructure^> (prm1);
        _Cprm1->Lock();
        QuantLib::DefaultProbabilityTermStructure* _prm1 = static_cast<QuantLib::DefaultProbabilityTermStructure*> (_Cprm1->GetPointer ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSurvivalProbability)->initialValue ( _prm1 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
UInt64 Cephei::QL::Termstructures::Credit::CSurvivalProbability::MaxIterations::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppSurvivalProbability)->maxIterations ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Credit::ISurvivalProbability^ Cephei::QL::Termstructures::Credit::CSurvivalProbability::UpdateGuess (Cephei::Core::IVector<Double>^ data, Double p, UInt64 i)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        data->Lock();
        INativeVector<Double>^ _NCIdata = data->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdata = safe_cast<CDoubleVector^>(_NCIdata);
        std::vector<QuantLib::Real>& _data = static_cast<std::vector<QuantLib::Real>&> (_NCdata->GetReference ());
        QuantLib::Probability _p = (QuantLib::Probability)ValueHelper::Convert (p); //a
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	(*_ppSurvivalProbability)->updateGuess ( _data,  _p,  _i );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (data != nullptr) data->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

